<template>
  <el-dialog
    :visible="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="title"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="form.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <el-option v-for="item in simpleList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="updateForm">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getSimple, getDepartment, addDepartment, departmentDetails, editDepartment } from '@/api/department'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    current: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      rules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '必须2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            const res = await getDepartment()
            if (this.title === '修改部门') {
              res.data = res.data.filter(i => i.id !== this.form.id)
            }
            res.data.some(i => i.code === value) ? callback('部门编码不能重复') : callback()
          }, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '必须2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            const res = await getDepartment()
            if (this.title === '修改部门') {
              res.data = res.data.filter(i => i.id !== this.form.id)
            }
            res.data.some(i => i.name === value) ? callback('部门名称不能重复') : callback()
          }, trigger: 'blur' }
        ]
      },
      simpleList: []
    }
  },
  created() {
    this.getSimple()
  },
  methods: {
    close() {
      this.$emit('update:visible', false)
      this.$refs.formRef.resetFields()
    },
    async getSimple() {
      const res = await getSimple()
      this.simpleList = res.data
    },
    hsave() {
      this.$refs.formRef.validate(valid => {
        if (valid) this.updateForm()
      })
    },
    async updateForm() {
      if (this.title === '新增部门') {
        this.form.pid = this.current
        const res = await addDepartment(this.form)
        this.$message.success(res.message)
      } else if (this.title === '修改部门') {
        const res = await editDepartment(this.form)
        this.$message.success(res.message)
        this.form = {
          code: '',
          introduce: '',
          managerId: '',
          name: '',
          pid: ''
        }
      }
      this.$emit('refresh')
      this.close()
    },
    async getDepartmentDetails() {
      const res = await departmentDetails(this.current)
      this.form = res.data
    }
  }
}
</script>

<style>
</style>
